package com.lebang.service;

import com.lebang.domain.MemberWallet;
import com.lebang.mapper.LbbMemberTradeMapper;
import com.lebang.mapper.MemberCashWithdrawalMapper;
import com.lebang.mapper.MemberWalletMapper;
import com.lebang.model.enums.CheckStatus;
import com.lebang.model.enums.buss.RewardType;
import com.lebang.model.enums.buss.TradeCategory;
import com.lebang.model.enums.buss.TradeStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: hwq
 * @Date: 2020/4/23 10:25
 * @Version 1.0
 */
@Service
@Slf4j
public class MemberWalletService {
    @Autowired
    private MemberWalletMapper memberWalletMapper;
    @Autowired
    private LbbMemberTradeMapper lbbMemberTradeMapper;
    @Autowired
    private MemberCashWithdrawalMapper memberCashWithdrawalMapper;
    private static final BigDecimal HUNDRED = BigDecimal.valueOf(100);
    public MemberWallet selectWallet(String memberId){
        return memberWalletMapper.selectByPrimaryKey(memberId);
    }

    public void incomeMoney(String memberId, int amount) {
        try {
            MemberWallet wallet = this.getWalter(memberId);
            boolean status = true;
            int k=0;
            while (status) {
                k++;
                int i = memberWalletMapper.IncomeMoney(memberId, amount, wallet.getRevision());
                if(k>1){
                    log.error("用户id:"+memberId+"------->>第{"+k+":次}");
                }
                if (i > 0) {
                    status = false;
                } else {
                    wallet.setRevision(memberWalletMapper.selectRevision(memberId));status = false;
                }

            }
        }catch (Exception e){
            log.error("用户id:"+memberId);
            e.printStackTrace();
        }


    }

    private MemberWallet getWalter(String memberId) {
            return memberWalletMapper.selectByPrimaryKey(memberId);
    }

    public void outLayMoney(String memberId, int amount) {
        MemberWallet wallet = null;
        try {
             wallet = this.getWalter(memberId);
            boolean status = true;
            int k=0;
            while (status) {
                k++;
                if(k>1){
                    log.error("用户id:"+memberId+"------->>第{"+k+":次}");
                }
                int i = memberWalletMapper.OutLayMoney(memberId, amount, wallet.getRevision());
                if (i > 0) {
                    status = false;
                } else {
                    wallet.setRevision(memberWalletMapper.selectRevision(memberId));
                }
            }
        }catch (Exception e){
            log.error("用户id:"+memberId);
            log.error("用户id:"+wallet);

            e.printStackTrace();
        }
    }



    public void addFail(String memberId) {
        memberWalletMapper.addFail(memberId);
    }

    public Long getMoneyByMemberId(String memberId) {
        MemberWallet money = this.getWalter(memberId);
       return money.getMoney();
    }

    public Long getAllMoneyByMemberId(String memberId){
        MemberWallet money = this.getWalter(memberId);
        return money.getAllMoney();
    }

    public void initData(String memberId) {
            Integer lift = lbbMemberTradeMapper.sum(memberId, "xianyu", TradeCategory.Outlay.name(), RewardType.Cash.name(), null, null);
            Integer all = lbbMemberTradeMapper.sum(memberId, "xianyu", TradeCategory.Income.name(), RewardType.Cash.name(), null, null);
            Integer successCount = lbbMemberTradeMapper.OutlaySuccess(memberId, TradeStatus.Success.name());
            Integer failCount = lbbMemberTradeMapper.OutlaySuccess(memberId, TradeStatus.Invalid.name())+
                    memberCashWithdrawalMapper.selectByStatusAndMemberId(memberId, CheckStatus.FAIL.getIndex());
            MemberWallet wallet = new MemberWallet();
            wallet.setCreateTime(LocalDateTime.now());
            wallet.setModifyTime(LocalDateTime.now());
            wallet.setHasDeleted(0);
            wallet.setSuccessCount(successCount);
            wallet.setMemberId(memberId);
            wallet.setFailCount(failCount);
            wallet.setAllMoney(all.longValue());
            wallet.setAllCash(lift.longValue());
            Long value=all.longValue()-lift.longValue();
            wallet.setMoney(value);
            wallet.setRebateMoney(0);
            wallet.setRevision(0L);
            memberWalletMapper.insertSelective(wallet);
    }

    public List<MemberWallet> selectNegativeMoney() {
        return memberWalletMapper.selectNegativeMoney();
    }

    public void deleteById(String memberId){
        memberWalletMapper.deleteByPrimaryKey(memberId);
    }
}
